package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.config.redssion.RedissonConfig;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {


    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public Map<String, Object> getItem(Long albumId) {
        //创建一个map集合
        HashMap<String, Object> map = new HashMap<>();

        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //获取远程调用结果集
            Result<AlbumInfo> infoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(infoResult,"远程调用获取专辑信息失败");
            AlbumInfo albumInfo = infoResult.getData();
            Assert.notNull(albumInfo,"远程调用获取专辑信息失败");
            //将数据存储
            map.put("albumInfo",albumInfo);
            return albumInfo;
        },threadPoolExecutor).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return new AlbumInfo();
        });
        //获取分类信息
        CompletableFuture<Integer> categoryCompletableFuture = albumInfoCompletableFuture.thenApplyAsync(albumInfo -> {
            //  远程调用获取分类信息;
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(categoryViewResult, "远程调用获取分类视图失败");
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            Assert.notNull(baseCategoryView, "远程调用获取分类视图失败");
            map.put("baseCategoryView", baseCategoryView);
            //  默认处理成功返回1；
            return 1;
        },threadPoolExecutor).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return 0;
        });
        //获取主播信息
        CompletableFuture<Integer> userCompletableFuture = albumInfoCompletableFuture.thenApplyAsync(albumInfo -> {
            //  远程调用获取数据
            Result<UserInfoVo> infoVoResult = userInfoFeignClient.getUserInfo(albumInfo.getUserId());
            Assert.notNull(infoVoResult, "远程调用获取用户信息失败");
            UserInfoVo userInfoVo = infoVoResult.getData();
            Assert.notNull(userInfoVo, "远程调用获取用户信息失败");
            map.put("announcer", userInfoVo);
            //  默认处理成功返回1；
            return 1;
        },threadPoolExecutor).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return 0;
        });
        //根据专辑id获取专辑统计信息
        CompletableFuture<Integer> statCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //  远程调用
            Result<AlbumStatVo> statVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(statVoResult, "远程调用获取专辑统计信息失败");
            AlbumStatVo albumStatVo = statVoResult.getData();
            Assert.notNull(albumStatVo, "远程调用获取专辑统计信息失败");
            map.put("albumStatVo", albumStatVo);
            //  成功 默认返回 1
            return 1;
        },threadPoolExecutor).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return 0;
        });
        //  获取每个线程执行的结果;
        try {
            if (null == albumInfoCompletableFuture.get() || 0 == categoryCompletableFuture.get() ||
                    0 == userCompletableFuture.get() || 0 == statCompletableFuture.get()) {
                //  返回数据
                return map;
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        //  多任务组合;
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                categoryCompletableFuture,
                userCompletableFuture,
                statCompletableFuture
        ).join();
        //返回数据
        return map;
    }
}
